<?php
class Role extends MY_SecuredController {
    public function __construct() {
        parent::MY_SecuredController();

        $this->load->helper(array('url', 'language', 'form'));
        $this->lang->load('home');
        
        $this->requireRole('Owner');
    }
    
    public function index() {
        $this->_preRender(Current_User::user()->id);
        
        $this->_render_index($data = array());
    }

    public function admins($op, $civlid = null) {
        $this->_preRender(Current_User::user()->id);

        $role = RoleModel::findByName('Admin');
        
        try {
            if ($op == 'add') {
                $role->addAdmin($this->input->post('admin_id'));
            }
            else if ($op == 'delete') {
                $role->deleteAdmin($civlid);
            }
            else if ($op == 'search') {
                $results = PersonModel::locatePerson($this->input->post('admin_term'));
                $options = array();
                $admins = array();
                 
                foreach ($role->Persons as $ca) {
                    $admins[] = $ca->id;
                }
                 
                foreach ($results as $k => $a) {
                    if (!in_array($a['id'], $admins)) {
                        $options[$a['id']] = $a['name'];
                    }
                }
                 
                $data['admin_search_results'] = $options;
                $this->_render_index($data);
                return;
            }
        }
        catch (Exception $e) {
            $this->redirect('owner/role/index', array(
                'message' => 'Ooops, the operation '.$op.' could not performed - try again later',
                'exception' => $e), true);
            return;
        }

        $this->redirect('owner/role/index');
    }
    
    public function owners($op, $civlid = null) {
        $this->_preRender(Current_User::user()->id);

        $role = RoleModel::findByName('Owner');
        
        try {
            if ($op == 'add') {
                $role->addOwner($this->input->post('owner_id'));
            }
            else if ($op == 'delete') {
                $role->deleteOwner($civlid);
            }
            else if ($op == 'search') {
                $results = PersonModel::locatePerson($this->input->post('owner_term'));
                $options = array();
                $owners = array();
                 
                foreach ($role->Persons as $ca) {
                    $owners[] = $ca->id;
                }
                 
                foreach ($results as $k => $a) {
                    if (!in_array($a['id'], $owners)) {
                        $options[$a['id']] = $a['name'];
                    }
                }
                 
                $data['owner_search_results'] = $options;
                $this->_render_index($data);
                return;
            }
        }
        catch (Exception $e) {
            $this->redirect('owner/role/index', array(
                'message' => 'Ooops, the operation '.$op.' could not performed - try again later',
                'exception' => $e), true);
            return;
        }

        $this->redirect('owner/role/index');
    }
    
    private function _render_index(&$data) {
        $data['person'] = Current_User::user();
        $data['users'] = RoleModel::findByName('User')->Persons;

        $admins = RoleModel::findByName('Admin')->Persons;
        $owners = RoleModel::findByName('Owner')->Persons;
        
        $data['admins'] = $admins;
        $data['owners'] = $owners;

        $this->render('owner/roles/index', $data);
    }
}

?>